<template>
  <div>
    <div
      class="enterprise_item"
      v-for="(item, index) in enterpriseData"
      :key="index"
      v-loading="loading"
    >
      <div class="enterprise_item_L">
        <!-- <div class="enterprise_img">
          <img
            :src="`https://bootapi.51bmj.cn/sys/common/static/${item.logo}`"
            alt=""
          />
        </div>-->
        <div class="enterprise_ctn">
          <div class="enterprise_ctn_t">
            <h3>{{ item.companyName }}</h3>
            <span>关注时间: {{ item.createTime }}</span>
          </div>
          <div class="enterprise_ctn_b">
            <span>荣誉{{ item.currentHonorCount }}个</span>
            <i v-if="item.honorAdded > 0">+{{ item.honorAdded }} new</i>
            <span>专利{{ item.currentPatentCount }}个</span>
            <i v-if="item.patentAdded > 0">+{{ item.patentAdded }} new</i>
            <span>商标{{ item.currentTrademarkCount }}个</span>
            <i v-if="item.trademarkAdded > 0">+{{ item.trademarkAdded }} new</i>
            <span>软著{{ item.currentSoftCount }}个</span>
            <i v-if="item.softworkAdded > 0">+{{ item.softworkAdded }} new</i>
          </div>
        </div>
      </div>
      <div
        class="enterprise_btn"
        @click="cancelFollow(item.delFlag, index)"
        :class="item.delFlag ? 'active' : ''"
      >{{ item.delFlag ? " 恢复关注" : "已关注" }}</div>
    </div>
    <div class="pageList">
      <paging ref="paging_zi" @CurrentPage="accept_CurrentValue" />
    </div>
  </div>
</template>

<script>
import paging from "../paging.vue";
import {
  commonRequest,
  postRequest,
  postRequestSplicing
} from "../../api/api.js";
export default {
  name: "enterprise",
  components: {
    paging
  },
  data() {
    return {
      enterpriseData: [],
      // 当前页
      followIndex: 0,
      loading: true // 添加loading
    };
  },
  mounted() {
    this.followRequests();
  },
  methods: {
    // 数据获取
    followRequests() {
      var that = this;
      this.loading = true;
      commonRequest("/api/cms/cmsCompanyAttention/getMyCompanyAttention", {
        pageNo: that.followIndex
      }).then(res => {
        this.loading = false;
        for (let i = 0; i < res.records.length; i++) {
          // 荣誉计算
          if (
            res.records[i].currentHonorCount != null &&
            res.records[i].honorCount != null
          ) {
            res.records[i].honorAdded =
              res.records[i].currentHonorCount - res.records[i].honorCount;
          }
          // 专利计算
          if (
            res.records[i].currentPatentCount != null &&
            res.records[i].patentCount != null
          ) {
            res.records[i].patentAdded =
              res.records[i].currentPatentCount - res.records[i].patentCount;
          }
          // 商标计算
          if (
            res.records[i].currentTrademarkCount != null &&
            res.records[i].trademarkCount != null
          ) {
            res.records[i].trademarkAdded =
              res.records[i].currentTrademarkCount -
              res.records[i].trademarkCount;
          }
          // 软著计算
          if (
            res.records[i].currentSoftCount != null &&
            res.records[i].softNumber != null
          ) {
            res.records[i].softworkAdded =
              res.records[i].currentSoftCount - res.records[i].softNumber;
          }
        }
        that.enterpriseData = res.records;
        that.$refs.paging_zi.totalValue(res.total);
      });
    },
    // 是否取消关注
    cancelFollow(type, index) {
      if (type == 1) {
        postRequest(
          "/api/cms/cmsCompany/attentionCompany?id=" +
            this.enterpriseData[index].companyId
        ).then(res => {
          if (res.code == 200) {
            this.enterpriseData[index].delFlag = 0;
            this.$message({
              type: "success",
              message: "关注成功"
            });
          }
        });
      } else {
        this.$confirm("是否取消订阅?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.followFalse(index);
        });
      }
    },
    // 点击关注按钮
    followFalse(index) {
      postRequestSplicing(
        "/api/cms/cmsCompany/cancelAttentionCompany",
        `id=${this.enterpriseData[index].companyId}`
      ).then(res => {
        if (res.success) {
          this.enterpriseData[index].delFlag = 1;
          this.$message.success({
            message: "取消成功",
            type: "success"
          });
        } else {
          this.$message({
            showClose: true,
            message: "取消失败",
            type: "error"
          });
        }
      });
    },
    // 接受当前页
    accept_CurrentValue: function(value) {
      this.followIndex = value;
      this.followRequests();
    }
  }
};
</script>

<style lang="less" scoped>
.enterprise_item {
  height: 100px;
  width: 100%;
  padding: 0 30px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #eee;
  .enterprise_item_L {
    display: flex;
    align-items: center;
    .enterprise_img {
      img {
        width: 60px;
        height: 60px;
      }
    }
    .enterprise_ctn {
      display: flex;
      height: 100px;
      margin-left: 10px;
      justify-content: center;
      flex-direction: column;
      .enterprise_ctn_t {
        display: flex;
        align-items: center;
        margin: 5px 0;
        h3 {
          font-size: 18px;
        }
        span {
          font-size: 14px;
          margin-left: 20px;
        }
      }
      .enterprise_ctn_b {
        margin: 5px 0;
        span {
          color: #44a2ff;
          background-color: #e8f3ff;
          padding: 4px 8px;
          font-size: 12px;
          margin-right: 10px;
        }
        i {
          color: #f24d3a;
          font-style: normal;
          margin-right: 20px;
          font-size: 12px;
        }
      }
    }
  }

  .enterprise_btn {
    border: 1px solid #ccc;
    color: #ccc;
    background-color: #fff;
    padding: 4px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    &.active {
      background-color: #44a2ff;
      color: #fff;
      border: none;
    }
  }
}
.pageList {
  height: 100px;
  display: flex;
  flex-direction: row-reverse;
  align-items: center;
}
</style>
